/**
 * Created on [2022-12-10] by zhengrr
 *
 * 项目名称: btfvilg
 *
 * 本程序版权属于福建慧政通信息科技有限公司所有。
 *
 * 任何组织和个人未经福建慧政通信息科技有限公司许可与授权,不得擅自传播、复制、更改该程序的内容。
 * 本程序受版权法和国际条约的保护。如未经授权而擅自复制或传播本程序(或其中任何部分),
 * 将受到严厉的刑事及民事制裁，并将在法律许可的范围内受到最大可能的起诉!
 *
 * ©2021 福建慧政通信息科技有限公司
 */
package com.sunnada.btfvilg.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sunnada.btfvilg.entity.Sentence;
import com.sunnada.btfvilg.mapper.SentenceMapper;
import com.sunnada.btfvilg.service.SentenceService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Random;

/**
 *  服务实现类
 *
 * @author zhengrr
 */
@Service
public class SentenceServiceImpl extends ServiceImpl<SentenceMapper, Sentence> implements SentenceService {

    private List<Sentence> sentences = null;

    @Autowired(required = false)
    private SentenceMapper sentenceMapper;

    private String basePath = "D:\\work\\0-fzu\\1-课程\\1-英语\\单词\\";
    private Integer site = 0;

    @Override
    public Sentence nextSentence() {
        if(sentences!=null && site==sentences.size()){
            resetSentence();
        }
        if(sentences==null){
            QueryWrapper<Sentence> qw = new QueryWrapper<>();
            qw.orderByAsc("turn");
            sentences = sentenceMapper.selectList(qw);
            site = 0;
            Random random = new Random();
            for (int i = 0; i < sentences.size(); i++) {
                sentences.get(i).setRandom(random.nextInt(20));
            }
            Collections.sort(sentences, new Comparator<Sentence>() {
                @Override
                public int compare(Sentence o1, Sentence o2) {
                    int temp = o1.getTurn() - o2.getTurn();
                    if(temp == 0) return o1.getRandom() - o2.getRandom();
                    else return temp;
                }
            });
        }
        Sentence sentence = sentences.get(site++);
        sentenceMapper.updateTurn(sentence.getId(), sentence.getTurn()+1);
        return sentence;
    }

    @Override
    public void resetSentence() {
        sentences = null;
        site = 0;
    }

    @Override
    public Integer loadNewSentence() {
        int sum = 0;
        Integer minTurn = sentenceMapper.getMinTurn();
        if(minTurn == null) minTurn = 0;
        try {
            String line;
            BufferedReader br = new BufferedReader(
                    new InputStreamReader(
                            new FileInputStream(basePath+"loadNewSentence\\newSentence.txt"),"utf-8"
                    )
            );
            while((line=br.readLine()) != null){
                String[] split = line.split("=");
                if(split.length != 2) return 99999;
                try {
                    sentenceMapper.insert(new Sentence(DigestUtils.md5Hex(split[0]),split[0],split[1],minTurn,0));
                    sum++;
                } catch (Exception e) {
                    System.out.println(e.toString());
                }
            }
        } catch (Exception e) {
            System.out.println(e.toString());
        }
        return sum;
    }
}
